package com.mornd.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mornd.gulimall.product.entity.Brand;
import com.mornd.gulimall.product.entity.Category;
import com.mornd.gulimall.product.entity.CategoryBrandRelation;
import com.mornd.gulimall.product.mapper.CategoryBrandRelationMapper;
import com.mornd.gulimall.product.service.IBrandService;
import com.mornd.gulimall.product.service.ICategoryBrandRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mornd.gulimall.product.service.ICategoryService;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 品牌分类关联 服务实现类
 * </p>
 *
 * @author mornd
 * @since 2023-07-23
 */
@Service
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelation> implements ICategoryBrandRelationService {
    @Resource
    private ObjectFactory<IBrandService> brandServiceFactory;
    @Resource
    private ObjectFactory<ICategoryService> categoryServiceFactory;

    @Override
    public IPage<CategoryBrandRelation> cateGoryList(CategoryBrandRelation relation) {
        IPage<CategoryBrandRelation> page = new Page<>(relation.validPageNum(), relation.validPageSize());

        LambdaQueryWrapper<CategoryBrandRelation> qw = Wrappers.<CategoryBrandRelation>lambdaQuery()
                .eq(relation.getBrandId() != null, CategoryBrandRelation::getBrandId, relation.getBrandId());

        return super.page(page, qw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSave(CategoryBrandRelation entity) {
        entity.setId(null);
        Brand brand = brandServiceFactory.getObject().getById(entity.getBrandId());
        if(brand == null) {
            return false;
        }

        // 再批量保存
        List<CategoryBrandRelation> batch = new ArrayList<>();
        for (Long catId : entity.getCatelogIdList()) {
            // 如果已存在关系则不添加
            long count = super.count(Wrappers.<CategoryBrandRelation>lambdaQuery().eq(CategoryBrandRelation::getBrandId, entity.getBrandId()).eq(CategoryBrandRelation::getCatelogId, catId));
            if(count > 0) {
                continue;
            }

            CategoryBrandRelation save = new CategoryBrandRelation();
            save.setBrandId(brand.getBrandId());
            save.setBrandName(brand.getName());

            Category category = categoryServiceFactory.getObject().getById(catId);
            if (category != null) {
                save.setCatelogId(category.getCatId());
                save.setCatelogName(category.getName());
                batch.add(save);
            }
        }

        return super.saveBatch(batch);
    }

    @Override
    public void updateBrand(Brand brand) {
        LambdaUpdateWrapper<CategoryBrandRelation> uw = Wrappers.<CategoryBrandRelation>lambdaUpdate()
                .set(CategoryBrandRelation::getBrandName, brand.getName())
                .eq(CategoryBrandRelation::getBrandId, brand.getBrandId());
        super.update(uw);
    }

    @Override
    public void updateCategory(Category category) {
        LambdaUpdateWrapper<CategoryBrandRelation> uw = Wrappers.<CategoryBrandRelation>lambdaUpdate()
                .set(CategoryBrandRelation::getCatelogName, category.getName())
                .eq(CategoryBrandRelation::getCatelogId, category.getCatId());
        super.update(uw);
    }
}
